﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Dormsystem
{
    public partial class UserList : Form
    {
        public UserList()
        {
            InitializeComponent();
        }

        private void UserList_Load(object sender, EventArgs e)
        {

            DisplayUsers();
        }

        private void tsmiDeleteUser_Click(object sender, EventArgs e)
        {
            if (lvUserList.SelectedItems.Count>0)  //有用户选中
            {
                int i = 0;
                int cnt = lvUserList.SelectedItems.Count;

                while(cnt>0)
                {              
                    string username = lvUserList.SelectedItems[i].Text;
                    i++;
                    cnt--;

                    if (MessageBox.Show("是否删除" + username, "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string sql = string.Format("delete from userinfo where username='{0}'",username);

                        int result = Convert.ToInt32(DB.ExcuteSQL(sql));

                    }
                }

                DisplayUsers();
            }
        }

        public void DisplayUsers()
        {
            string sql = "select * from userinfo;";

            SqlDataReader rd = DB.GetDataReader(sql);

            lvUserList.Items.Clear();

            while (rd.Read())
            {
                string username = rd["UserName"].ToString();
                string pwd = rd["Password"].ToString();
                string state = rd["UserState"].ToString();

                //添加到ListView中
                ListViewItem lvi = new ListViewItem(username);
                lvi.SubItems.Add(pwd);
                lvi.SubItems.Add(state);

                lvUserList.Items.Add(lvi);
            }

            rd.Close();
        }

        private void tsmiRefresh_Click(object sender, EventArgs e)
        {
            DisplayUsers();
        }

        private void tsmiStart_Click(object sender, EventArgs e)
        {
            ChangeState("正常");
        }

        private void tsmiStop_Click(object sender, EventArgs e)
        {
            ChangeState("暂停");
        }

        private void ChangeState(string state)
        {
            if (lvUserList.SelectedItems.Count > 0)  //有用户选中
            {
                string username = lvUserList.SelectedItems[0].Text;

                string sql = string.Format("update userinfo set userstate = '{0}' where username='{1}'", state, username);

                int result = Convert.ToInt32(DB.ExcuteSQL(sql));

                DisplayUsers();
            }
        }

        private void tsmiChangePwd_Click(object sender, EventArgs e)
        {
            if (lvUserList.SelectedItems.Count>0)
            {
                string userName = lvUserList.SelectedItems[0].Text;

                ChangePassword changepassword = new ChangePassword();
                changepassword.lblUserName.Text = userName;

                if (changepassword.ShowDialog() == DialogResult.OK)
                {
                    DisplayUsers();
                }
            }
        }
    }
}
